﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lanting.Utilities;

namespace LantingConnect
{
    public partial class UC_PageNavication : UserControl
    {
        #region 全局变量、常量
        /// <summary>
        /// 请求页码
        /// </summary>
        public int opsPageNo { get; set; }
        /// <summary>
        /// 当前页码
        /// </summary>
        public int pageNo { get; set; }
        /// <summary>
        /// 每页条数
        /// </summary>
        private int _pageSize;
        public int pageSize
        {
            get
            {
                Function_RefreshPageSize();
                return _pageSize;
            }
            set
            {
                _pageSize = value;
            }
        }
        /// <summary>
        /// 总页数
        /// </summary>
        public int totalPages { get; set; }
        /// <summary>
        /// 总条数
        /// </summary>
        public int totalDatas { get; set; }

        private const string strFormat = "共 {0} 条记录，每页 {1} 条，共 {2} 页";

        //声明委托
        public delegate void OnNextPageClickEventHandler(object sender, EventArgs e);
        public delegate void OnPreviousClickEventHandler(object sender, EventArgs e);
        public delegate void OnFirstPageClickEventHandler(object sender, EventArgs e);
        public delegate void OnLastPageClickEventHandler(object sender, EventArgs e);
        public delegate void OnEnterPressEventHandler(object sender, EventArgs e);
        public delegate void OnRefreshClickEventHandler(object sender, EventArgs e);
        //定义事件
        public event OnNextPageClickEventHandler onNextPageClick;
        public event OnPreviousClickEventHandler onPreviousClick;
        public event OnFirstPageClickEventHandler onFirstPageClick;
        public event OnLastPageClickEventHandler onLastPageClick;
        public event OnEnterPressEventHandler onEnterPress;
        public event OnRefreshClickEventHandler onRefreshClick;
        #endregion

        #region 用户控件初始化
        public UC_PageNavication()
        {
            InitializeComponent();

            Function_InitPageNavigation();
        }
        #endregion

        #region 用户控件事件    

        #region 事件-上一页按钮点击
        /// <summary>
        /// 上一页按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_previous_Click(object sender, EventArgs e)
        {
            Function_RefreshPageSize();

            this.opsPageNo -= 1;
            this.resetValues();
            onPreviousClick?.Invoke(sender, e);
        }
        #endregion

        #region 事件-第一页按钮点击
        /// <summary>
        /// 第一页按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>        
        private void bt_first_Click(object sender, EventArgs e)
        {
            Function_RefreshPageSize();

            this.opsPageNo = 1;
            this.resetValues();
            onFirstPageClick?.Invoke(sender, e);
        }
        #endregion

        #region 事件-下一页按钮点击
        /// <summary>
        /// 下一页按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_next_Click(object sender, EventArgs e)
        {
            Function_RefreshPageSize();

            this.opsPageNo += 1;
            this.resetValues();
            onNextPageClick?.Invoke(sender, e);
        }
        #endregion

        #region 事件-最后一页按钮点击
        /// <summary>
        /// 最后一页按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_last_Click(object sender, EventArgs e)
        {
            Function_RefreshPageSize();

            this.opsPageNo = this.totalPages;
            this.resetValues();
            onLastPageClick?.Invoke(sender, e);
        }
        #endregion

        #region 事件-键盘按键(回车)
        /// <summary>
        /// 键盘按键事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void text_pageNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                Function_RefreshPageSize();

                this.opsPageNo = Int32.Parse(text_pageNo.Text);
                this.resetValues();
                onEnterPress?.Invoke(sender, e);
            }
        }
        #endregion

        #region 事件-刷新按钮点击
        /// <summary>
        /// 刷新按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_refresh_Click(object sender, EventArgs e)
        {
            Function_RefreshPageSize();

            this.opsPageNo = this.pageNo;
            this.resetValues();
            onRefreshClick?.Invoke(sender, e);
        }
        #endregion

        #endregion

        #region 方法-刷新每页条数
        private void Function_RefreshPageSize()
        {
            if (combo_pageSize.Text != "全部")
            {
                this.pageSize = Int32.Parse(combo_pageSize.Text);
            }
            else
            {
                this.pageSize = Params.PageSizeMax;
            }
        }
        #endregion

        #region 方法-分页控件初始化
        public void Function_InitPageNavigation()
        {
            opsPageNo = 1;
            pageNo = 1;
            totalPages = 1;
            totalDatas = 0;
            pageSize = Params.PageSize;
            refreshStyles();
        }
        #endregion

        #region 方法-刷新控件界面样式
        /// <summary>
        /// 刷新控件界面样式
        /// </summary>
        public void refreshStyles()
        {
            //根据参数值刷新界面样式
            if (pageNo <= 1)
            {
                bt_previous.Enabled = false;
                bt_first.Enabled = false;
            }
            else
            {
                bt_previous.Enabled = true;
                bt_first.Enabled = true;
            }

            if (pageNo >= totalPages)
            {
                bt_next.Enabled = false;
                bt_last.Enabled = false;
            }
            else
            {
                bt_next.Enabled = true;
                bt_last.Enabled = true;
            }
            string[] arg = new string[3];
            arg[0] = totalDatas.ToString();
            //不分页时，显示
            if (pageSize == Params.PageSizeMax)
            {
                arg[1] = "全部";
                arg[2] = "1";
            }
            else
            {
                arg[1] = pageSize.ToString();
                arg[2] = totalPages.ToString();
            }
            label.Text = String.Format(strFormat, arg);
            text_pageNo.Text = pageNo.ToString();
        }
        #endregion

        #region 方法-刷新控件参数
        /// <summary>
        /// 刷新控件参数值
        /// </summary>
        public void refreshParams(int _pageNo, int _pageSize, int _totalPages, int _totalDatas)
        {
            pageNo = _pageNo;
            pageSize = _pageSize;
            totalPages = _totalPages;
            totalDatas = _totalDatas;
        }
        #endregion

        #region 方法-分页处理
        public void Function_RefreshPageNavigation(int _pageSize, int _totalDatas)
        {
            //分页处理
            totalDatas = _totalDatas;
            pageSize = _pageSize;
            if (totalDatas % pageSize != 0)
            {
                totalPages = (_totalDatas / pageSize) + 1;
            }
            else
            {
                totalPages = _totalDatas / pageSize;
            }
            pageNo = opsPageNo;
        }
        #endregion

        #region 方法-修正控件参数
        public void resetValues()
        {
            //修正各参数值
            if (totalPages <= 0)
            {
                totalPages = 1;
            }
            if (totalDatas < 0)
            {
                totalDatas = 0;
            }

            if (opsPageNo < 1)
            {
                opsPageNo = 1;
            }
            if (opsPageNo > totalPages)
            {
                opsPageNo = totalPages;
            }
        }
        #endregion

    }
}
